{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# !wget https://s3.amazonaws.com/opennmt-trainingdata/baseline-1M-enfr.tgz\n",
    "# !tar -zxf baseline-1M-enfr.tgz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['baseline-1M-enfr/baseline-1M_test.en',\n",
       " 'baseline-1M-enfr/baseline-1M_valid.en',\n",
       " 'baseline-1M-enfr/baseline-1M_train.fr',\n",
       " 'baseline-1M-enfr/baseline-1M_valid.fr',\n",
       " 'baseline-1M-enfr/baseline-1M_train.en',\n",
       " 'baseline-1M-enfr/baseline-1M_test.fr']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glob import glob\n",
    "\n",
    "files = glob('baseline-1M-enfr/*')\n",
    "files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1009163, 1009163)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('baseline-1M-enfr/baseline-1M_train.en') as fopen:\n",
    "    train_en = fopen.read().split('\\n')[:-1]\n",
    "    \n",
    "with open('baseline-1M-enfr/baseline-1M_train.fr') as fopen:\n",
    "    train_fr = fopen.read().split('\\n')[:-1]\n",
    "    \n",
    "len(train_en), len(train_fr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 1000)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('baseline-1M-enfr/baseline-1M_test.en') as fopen:\n",
    "    test_en = fopen.read().split('\\n')[:-1]\n",
    "    \n",
    "with open('baseline-1M-enfr/baseline-1M_test.fr') as fopen:\n",
    "    test_fr = fopen.read().split('\\n')[:-1]\n",
    "    \n",
    "len(test_en), len(test_fr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2000, 2000)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('baseline-1M-enfr/baseline-1M_valid.en') as fopen:\n",
    "    test_en.extend(fopen.read().split('\\n')[:-1])\n",
    "    \n",
    "with open('baseline-1M-enfr/baseline-1M_valid.fr') as fopen:\n",
    "    test_fr.extend(fopen.read().split('\\n')[:-1])\n",
    "    \n",
    "len(test_en), len(test_fr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1009163/1009163 [00:03<00:00, 301686.48it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(1009088, 1009088)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "train_X, train_Y = [], []\n",
    "\n",
    "for i in tqdm(range(len(train_en))):\n",
    "    if len(train_en[i].split()) > 100 or len(train_fr[i].split()) > 100:\n",
    "        continue\n",
    "    train_X.append(train_en[i])\n",
    "    train_Y.append(train_fr[i])\n",
    "    \n",
    "len(train_X), len(train_Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_X, test_Y = train_X[-5000:], train_Y[-5000:]\n",
    "train_X, train_Y = train_X[:200000], train_Y[:200000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "with open('dataset.json', 'w') as fopen:\n",
    "    json.dump({'train_X': train_X, 'train_Y': train_Y, 'test_X': test_X, 'test_Y': test_Y}, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
